<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;
/**
 * 广告管理
 * Class Ads
 * @package app\admin\controller
 */
class AdsNotice extends AdminBase
{
    public function index()
    {
        $list = Db::table('lsj_ads_notice')->select();
        return $this->fetch('index',['list'=>$list]);
    }

    public function add()
    {
        if($this->request->isPost()){
            $data['message'] = input('post.message');
            if(strlen($data['message'])<5){
                $this->error('输入文字过少');
            }
            $insert_result = Db::table('lsj_ads_notice')->insert($data);
            if($insert_result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            return $this->fetch('add');
        }
    }

    public function edit()
    {
        if($this->request->isPost()){
            $data['message'] = input('post.message');
            if(strlen($data['message'])<5){
                $this->error('输入文字过少');
            }
            $data['id'] = input('post.id');
            $data['updated_at'] = date("Y-m-d H:i:s",time());
            $update_result = Db::table('lsj_ads_notice')->update($data);
            if($update_result){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $id = input('id');
            if(empty($id)){
                $this->error('请选择要操作的id');
            }
            $list = Db::table('lsj_ads_notice')->where("id={$id}")->select();
            return $this->fetch('edit',['list'=>$list[0]]);
        }
    }

    public function del()
    {
        $id = input('id');
        if(empty($id)){
            $this->error('请选择要操作的id');
        }
        if(Db::table('lsj_ads_notice')->where("id={$id}")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}